// pages/xiang/xiang.js
import {
  shoppDetails,
} from '../../utils/api'

var WxParse = require('../../wxParse/wxParse')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topShow: true,
    topList: ['商品简介', '商品详情', '商品评价'],
    active: 0,
    toView: 'brief',
    briefTop: 0,
    detailsTop: 0,
    evaluateTop: 0,
    xiangList: [],
    showModal: false
  },

  //点击我显示底部弹出框
  clickme: function () {
    this.showModal();
  },
  shopcar() {

  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  onChange(e) {
    switch (e.detail.index) {
      case 0:
        this.setData({
          toView: 'brief'
        })
        break;
      case 1:
        this.setData({
          toView: 'details'
        })
        break;
      case 2:
        this.setData({
          toView: 'evaluate'
        })
        break;
    }
  },

  boxScroll(e) {
    if (e.detail.scrollTop <= this.data.detailsTop - this.data.briefTop - 80) {
      this.setData({
        active: 0
      })
    }
    if (e.detail.scrollTop > this.data.detailsTop - this.data.briefTop - 80) {
      this.setData({
        active: 1
      })
    }
    if (e.detail.scrollTop > this.data.evaluateTop - this.data.briefTop) {
      this.setData({
        active: 2
      })
    }

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(options);
    if (options.id == 904941) {
      this.setData({
        topShow: false
      })
    }

    shoppDetails({
      id: options.id,
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log(res);
      var article = res.data.data.content;
      WxParse.wxParse('article', 'html', article, this, 5);

      this.setData({
        xiangList: res.data.data
      })

      let query = wx.createSelectorQuery();
      query.select('#brief').boundingClientRect(res => {
        console.log(res.top);
        this.setData({
          briefTop: res.top
        })
      }).exec();

      query.select('#details').boundingClientRect(res => {
        console.log(res.top);
        this.setData({
          detailsTop: res.top
        })
      }).exec();

      query.select('#evaluate').boundingClientRect(res => {
        console.log(res.top);
        this.setData({
          evaluateTop: res.top
        })
      }).exec()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})